package project.service;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import project.mapper.TeacherMapper;
import project.pojo.Course;
import project.pojo.Teacher;
import project.util.SqlSessionFactoryUtils;

import java.util.List;

/**
 * @version: 1.0
 * @Auther: Ranger
 * @date: 2022-05-11 21:43
 */
public class TeacherService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    /**
     * 老师登入判断
     * @param id
     * @param password
     * @return
     */
    public Teacher judgeLogin(long id,String password){
        SqlSession sqlSession = sqlSessionFactory.openSession();

        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        Teacher teacher = mapper.judgeLogin(id, password);

        sqlSession.close();

        return teacher;
    }

    /**
     *
     * 展示老师教的课程
     * @param id
     * @return
     */
    public List<Course> showCourseByTeacherId(long id){
        SqlSession sqlSession = sqlSessionFactory.openSession();

        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        List<Course> course = mapper.showCourseByTeacherId(id);

        sqlSession.close();

        return course;
    }

    /**
     *
     * 选择给定条件的课程
     * @param name
     * @param courseTime
     * @param type
     * @return
     */
    public List<Course> queryAllByInformation(String name,String courseTime,Long teacherId,String type){
        SqlSession sqlSession = sqlSessionFactory.openSession();

        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        List<Course> course = mapper.queryAllByInformation(name,courseTime,teacherId,type);

        sqlSession.close();

        return course;
    }

    /**
     * 老师添加课程
     * @param course
     */
    public boolean addCourse(Course course){
        SqlSession sqlSession = sqlSessionFactory.openSession();

        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        boolean flag = mapper.addCourse(course);

        sqlSession.commit();

        sqlSession.close();

        return flag;
    }

    /**
     *
     * 查询老师信息
     * @param teacherId
     * @return
     */
    public Teacher selectTeacherByTeacherId(Long teacherId){
        SqlSession sqlSession = sqlSessionFactory.openSession();

        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        Teacher teacher = mapper.selectTeacherByTeacherId(teacherId);

        sqlSession.close();

        return teacher;
    }
    /**
     *
     * 根据课程id删除一个课程
     * @param courseId
     * @return
     */
    public boolean deleteCourseSingle(Long courseId){
        SqlSession sqlSession = sqlSessionFactory.openSession();

        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        boolean flag = mapper.deleteCourseSingle(courseId);

        sqlSession.commit();

        sqlSession.close();

        return flag;
    }

    /**
     *
     * 根据课程id修改一个课程
     * @param course
     * @return
     */
    public boolean updateCourseById(Course course){
        SqlSession sqlSession = sqlSessionFactory.openSession();

        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        boolean flag = mapper.updateCourseById(course);

        sqlSession.commit();

        sqlSession.close();

        return flag;
    }

    /**
     *
     * 批量删除课程
     * @param ids
     * @return
     */
    public boolean deleteCourseByIds(Long[] ids){
        SqlSession sqlSession = sqlSessionFactory.openSession();

        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        boolean flag = mapper.deleteCourseByIds(ids);

        sqlSession.commit();

        sqlSession.close();

        return flag;
    }

    /**
     * 修改老师信息
     * @param teacher
     * @return
     */
    public boolean updateTeacherInfo(Teacher teacher){
        SqlSession sqlSession = sqlSessionFactory.openSession();

        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        boolean flag = mapper.updateTeacherInfo(teacher);

        sqlSession.commit();

        sqlSession.close();

        return flag;
    }

    public boolean updateTeacherPassword(Long id, String password){
        SqlSession sqlSession = sqlSessionFactory.openSession();

        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        boolean flag = mapper.updateTeacherPassword(id,password);

        sqlSession.commit();

        sqlSession.close();

        return flag;
    }
}
